package com.meida.module.bus.provider.api.util;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.json.XML;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class PmsToolUtil {
    /**
     * Long型判空
     * @param val
     * @return
     */
    public static Boolean checkLong(Long val) {
        if (0L == val || null == val) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    /**
     * 获取Val
     * @param obj
     * @param method
     * @return
     */
    public static Object getValue(Object obj, String method) {
        Method taSet = ReflectUtil.getMethod(obj.getClass(), method);
        Object rs = null;
        try {
            rs = taSet.invoke(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(null == rs) {
            rs = new Object();
        }
        return rs;
    }

    /**
     * 获取Val
     * @param obj
     * @param method
     * @return
     */
    public static String getStrValue(Object obj, String method) {
        Method taSet = ReflectUtil.getMethod(obj.getClass(), method);
        Object rs = null;
        try {
            rs = taSet.invoke(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(null == rs) {
            return "";
        }
        return String.valueOf(rs);
    }

    /**
     * 获取Val
     * @param obj
     * @param method
     * @return
     */
    public static BigDecimal getBigDecimalValue(Object obj, String method) {
        if(obj == null) {
            return BigDecimal.ZERO;
        }
        Method taSet = ReflectUtil.getMethod(obj.getClass(), method);
        Object rs = null;
        try {
            rs = taSet.invoke(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(null == rs) {
            return BigDecimal.ZERO;
        }
        return new BigDecimal(String.valueOf(rs));
    }

    /**
     * 获取Val
     * @param obj
     * @param method
     * @return
     */
    public static Long getLongValue(Object obj, String method) {
        Method taSet = ReflectUtil.getMethod(obj.getClass(), method);
        Object rs = null;
        try {
            rs = taSet.invoke(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(null == rs) {
            return 0L;
        }
        return Long.valueOf(String.valueOf(rs));
    }

    /**
     * 获取Val
     * @param obj
     * @param method
     * @return
     */
    public static Integer getIntValue(Object obj, String method) {
        Method taSet = ReflectUtil.getMethod(obj.getClass(), method);
        Object rs = null;
        try {
            rs = taSet.invoke(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(null == rs) {
            return 999;
        }
        return Integer.valueOf(String.valueOf(rs));
    }

    /**
     * 设定Val
     * @param obj
     * @param method
     * @param val
     */
    public static void setValue(Object obj, String method, Object val) {
        try {
            ReflectUtil.invoke(obj, method, val);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * OA审批结果返回值解析
     *
     * @param xmlFile
     * @return
     */
    public static JSONObject getJSONObjectFromOAxml(String xmlFile) {
        JSONObject rs = null;
        System.out.println(xmlFile);
        JSONObject jsonObject = XML.toJSONObject(xmlFile);
        rs = jsonObject.getJSONObject("soap:Envelope").getJSONObject("soap:Body").getJSONObject("ns1:testResponse").getJSONObject("ns1:out");
        if (null == rs) {
            rs = JSONUtil.createObj();
        }
        return rs;
    }

    public static Long changeLongVal(Long value) {
        if(null == value) {
            return 0L;
        }
        return value;
    }

    /**
     * 将一组数据固定分组，每组n个元素
     * @param source 要分组的数据源
     * @param n      每组n个元素
     * @return
     */
    public static List<List<String>> fixedGrouping(List<String> source, int n) {

        if (null == source || source.size() == 0 || n <= 0){
            return null;
        }
        List<List<String>> result = new ArrayList<List<String>>();

        int sourceSize = source.size();
        int size = (source.size() / n) + 1;
        for (int i = 0; i < size; i++) {
            List<String> subset = new ArrayList<String>();
            for (int j = i * n; j < (i + 1) * n; j++) {
                if (j < sourceSize) {
                    subset.add(source.get(j));
                }
            }
            if (!CollectionUtils.isEmpty(subset)){
                result.add(subset);
            }
        }
        return result;
    }

    public static Boolean ckNullStr(String str) {
        if(StrUtil.isEmpty(str) && !"null".equals(str)) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
